<template>
  <div class="import-questions-excel">
    <TeacherNavbar title="Excel导入题库" />
    
    <el-form :model="form" label-width="120px" @submit.prevent="handleSubmit">
      <el-form-item label="选择科目">
        <el-select v-model="form.subjectId" placeholder="请选择科目" style="width: 300px;">
          <el-option
            v-for="subject in subjectList"
            :key="subject.subjectId"
            :label="subject.subjectName"
            :value="subject.subjectId">
          </el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="上传Excel文件">
        <el-upload
          class="upload-demo"
          drag
          :auto-upload="false"
          :on-change="handleFileChange"
          :show-file-list="true"
          accept=".xlsx,.xls"
        >
          <el-icon class="el-icon--upload"><upload-filled /></el-icon>
          <div class="el-upload__text">
            将文件拖到此处，或<em>点击上传</em>
          </div>
          <template #tip>
            <div class="el-upload__tip">
              请上传.xls或.xlsx格式的Excel文件
            </div>
          </template>
        </el-upload>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="handleSubmit" :disabled="!form.subjectId || !selectedFile">导入</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { UploadFilled } from '@element-plus/icons-vue'
import TeacherAPI from '@/api/teacher'
import TeacherNavbar from '@/components/TeacherNavbar.vue'

export default {
  name: 'ImportQuestionsExcel',
  components: {
    UploadFilled,
    TeacherNavbar
  },
  data() {
    return {
      form: {
        subjectId: ''
      },
      subjectList: [],
      selectedFile: null
    }
  },
  created() {
    // 获取科目列表
    this.fetchSubjectList()
    
    // 从路由参数获取科目ID
    const subjectId = this.$route.query.subjectId
    if (subjectId) {
      this.form.subjectId = parseInt(subjectId)
    }
  },
  methods: {
    // 获取科目列表
    async fetchSubjectList() {
      try {
        const res = await TeacherAPI.getSubjectList(1, 100)
        if (res.data.code === 200 && res.data.msg === 'success') {
          this.subjectList = res.data.data.list || []
        } else {
          this.$message.error('获取科目列表失败')
        }
      } catch (error) {
        console.error('获取科目列表失败:', error)
        this.$message.error('获取科目列表失败')
      }
    },

    // 处理文件选择
    handleFileChange(file) {
      this.selectedFile = file.raw
    },

    // 提交导入
    async handleSubmit() {
      // 验证表单
      if (!this.form.subjectId) {
        this.$message.error('请选择科目')
        return
      }
      
      if (!this.selectedFile) {
        this.$message.error('请选择要上传的Excel文件')
        return
      }
      
      try {
        // 发起导入请求
        const res = await TeacherAPI.importQuestionsExcel(this.selectedFile, this.form.subjectId)
        if (res.data.code === 200 && res.data.msg === 'success') {
          this.$message.success('题目导入成功')
          // 返回科目列表
          this.$router.push('/teacher/subjects')
        } else {
          this.$message.error('题目导入失败: ' + (res.data.data || res.data.message))
        }
      } catch (error) {
        console.error('导入题目失败:', error)
        this.$message.error('导入题目失败: ' + (error.response?.data?.message || error.message))
      }
    },

    // 取消操作
    handleCancel() {
      this.$router.push('/teacher/subjects')
    }
  }
}
</script>

<style scoped>
.import-questions-excel {
  padding: 20px;
}

.upload-demo {
  width: 360px;
}
</style>